package com.nan.hrm.controller;

import com.nan.hrm.dto.SmsCodeDto;
import com.nan.hrm.service.IVerifyCodeService;
import com.nan.hrm.util.AjaxResult;
import com.nan.hrm.util.exception.GlobalException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/verifycode")
public class VerifyCodeController {

    @Autowired
    private IVerifyCodeService verifyCodeService ;

    @GetMapping("/imageCode/{imageCodeKey}")
    public AjaxResult imageCode(@PathVariable("imageCodeKey")  String imageCodeKey){
        try {
            String baseImg = verifyCodeService.createVerifyCode(imageCodeKey);
            return AjaxResult.me().setResultObj(baseImg);
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setResultObj(e.getMessage());
        }
    }

    @PostMapping("/sendSmsCode")
    public AjaxResult sendSmsCode(@RequestBody SmsCodeDto smsCodeDto){
        try {
            String code = verifyCodeService.sendSmsCode(smsCodeDto);
            System.out.println("验证码为：" + code+",请在5分钟内使用");
            return AjaxResult.me();
        } catch (GlobalException e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setResultObj(e.getMessage()).setMessage(e.getMessage());
        }catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.me().setSuccess(false).setResultObj(e.getMessage()).setMessage(e.getMessage());
        }
    }

}
